# -*-coding: Utf-8 -*-
# @File : teacther_interface .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2023/6/12

'''
讲师接口层
'''
from lib import common
from interface import common_interface
from db import models
from conf import settings

logger_teacher = settings.get_logger('teacher')

# 讲师接口层层面的登录接口
def teacher_login_interface(username_teacher, password_teacher, login_code, login_code_input):
    flag, msg = common_interface.login_interface_roles(
        username=username_teacher,
        password=password_teacher,
        login_random_code=login_code,
        login_input_code=login_code_input,
        roles='teacher')
    if flag:
        logger_teacher.info(msg)
    else:
        logger_teacher.warning(msg)
    return flag, msg


# 讲师查看课程列表接口
def teacher_check_courses_interface(login_teacher_name):
    # （1） 实例化讲师对象
    teacher_obj = models.Teacher.select(login_teacher_name)

    # 获取讲师中的课程列表
    teacher_course_list = teacher_obj.show_course_list()

    # 判断课程列表是否有值
    if not teacher_course_list:
        flow = f'当前讲师{login_teacher_name} 无讲授课程'
        logger_teacher.warning(flow)
        return False, flow

    return True, teacher_course_list


# 讲师选择课程后保存课程接口
def add_course_interface(login_teacher_name, course_name):
    # （1） 实例化老师对象
    teacher_obj = models.Teacher.select(login_teacher_name)

    # 讲师的课程列表
    course_list_teacher = teacher_obj.course_list_from_teacher

    # (2) 判断当前已选择课程是否已存在
    if course_name in course_list_teacher:
        flow = f'当前课程:>>>{course_name}已存在,请再次确认!!'
        logger_teacher.warning(flow)
        return False, flow

    # （3） 课程不存在则调用讲师类方法进行添加课程
    teacher_obj.add_course(course_name)

    flow = f'当前课程:>>>{course_name} 添加成功!!!'
    logger_teacher.info(flow)
    return True, flow


# 获取对应课程下的对应的学生列表
def get_all_course_students(login_teacher_name, course_name):
    # (1) 实例化讲师对象
    teacher_onj = models.Teacher.select(login_teacher_name)
    # (2) 调用讲师的方法返回学生列表
    student_list_from_teacher = teacher_onj.get_student_list(course_name)

    # (3) 判断课程列表内是否有学生
    if not student_list_from_teacher:
        flow = f'当前课程下无学生报学!!'
        logger_teacher.warning(flow)
        return False, flow

    # (4) 如果有学生则返回学生列表
    return True, student_list_from_teacher


# 讲师修改学生分数接口
def change_student_score_interface(login_teacher_name, course_name, student_name, score_student):
    # （1） 实例化讲师对象
    teacher_obj = models.Teacher.select(login_teacher_name)
    # （2） 调用讲师中的方法进行课程中的学生的分数的修改
    teacher_obj.change_student_score(course_name, student_name, score_student)

    flow = f'当前学生{student_name} 修改成绩成功!!'
    logger_teacher.info(flow)
    return True, flow
